/**
 * @author nttdocomo
 */
define(function(require) {
	require("../../aries");
	require("./checkbox");
	require("../radioManager");
	aries.klass("aries.form.field.Radio", aries.form.field.Checkbox.extend({
		/**
		 * @property {Boolean} isRadio
		 * `true` in this class to identify an object as an instantiated Radio, or subclass thereof.
		 */
		isRadio : true,

		/**
		 * @cfg {String} uncheckedValue
		 * @private
		 */

		// private
		inputType : 'radio',
		ariaRole : 'radio',

		formId : null,

		/**
		 * If this radio is part of a group, it will return the selected value
		 * @return {String}
		 */
		getGroupValue : function() {
			var selected = this.getManager().getChecked(this.name, this.getFormId());
			return selected ? selected.inputValue : null;
		},

		// inherit docs
		onChange : function(newVal, oldVal) {
			var me = this, r, rLen, radio, radios;

			aries.form.field.Checkbox.prototype.onChange.apply(this, arguments);

			if (newVal) {
				radios = me.getManager().getByName(me.name, me.getFormId()).items;
				rLen = radios.length;

				for ( r = 0; r < rLen; r++) {
					radio = radios[r];

					if (radio !== me) {
						radio.setValue(false);
					}
				}
			}
		},

		// inherit docs
		getManager : function() {
			return aries.form.RadioManager;
		},

		/**
		 * Sets either the checked/unchecked status of this Radio, or, if a string value is passed, checks a sibling Radio
		 * of the same name whose value is the value specified.
		 * @param {String/Boolean} value Checked value, or the value of the sibling radio button to check.
		 * @return {Ext.form.field.Radio} this
		 */
		setValue : function(v) {
			var me = this, active;

			if (_.isBoolean(v)) {
				aries.form.field.Checkbox.prototype.setValue.apply(this,arguments);
			} else {
				active = me.getManager().getWithValue(me.name, v, me.getFormId()).getAt(0);
				if (active) {
					active.setValue(true);
				}
			}
			return me;
		}
	}))
})
